﻿Imports LinhKienMayTinh.CommonMethods

Public Class NewsList
    Inherits BasePage

#Region "Khao báo các biến toàn cục"

    Protected dsSource As DataSet

#End Region

#Region "Khai báo các event"

    ''' <summary>
    ''' Page_Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim nPage As Integer = 1
        Dim nNewsPerPage As Integer = 1
        Dim dsNewsList As DataSet
        Dim nTotalPage As Integer = 0
        Dim txtTag = String.Empty

        Try
            If Me.QueryString("tag") IsNot Nothing Then
                txtTag = Me.QueryString("tag")
            End If

            nPage = Nz(Me.QueryString("page"), 1)
            nNewsPerPage = Nz(AppSettings("NewsPerPage"), 10)
            nTotalPage = Math.Ceiling(GetNewsCount(txtTag) / nNewsPerPage)

            If nTotalPage > 0 AndAlso nPage > nTotalPage Then
                Context.Items("ErrorCode") = CommonConstants.LINK_NAME_NOT_EXIST
                Server.Transfer("~/Pages/Error.aspx")
            End If

            dsNewsList = GetNewsList(nPage, nNewsPerPage, txtTag)
            If dsNewsList IsNot Nothing AndAlso dsNewsList.Tables(0).Rows.Count > 0 Then

                Me.ucNewsList.DataSource = dsNewsList
                If IsNothing(Session(CommonConstants.C_USERNAME)) = False Then
                    Me.ucNewsList.HasEditButton = True
                End If

                Me.Header.Title = Me.AppSettings("SiteName") & " | " & "Tin tức"

                ucPagingBar.Url = ResolveUrl("~/tin-tuc")
                ucPagingBar.CurrentPage = nPage
                ucPagingBar.TotalPage = nTotalPage

                Me.DataBind()
            End If
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

    Private Sub ProductList_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim master As LinhKienMayTinhMasterPage = DirectCast(Me.Page.Master, LinhKienMayTinhMasterPage)
        master.CssLinkAdd(ResolveUrl("~/Style/NewsListMetroStyle.css"))
        master.CssLinkAdd(ResolveUrl("~/Style/PagingBarMetroStyle.css"))

        master.ScriptLinkAdd(ResolveUrl("~/Scripts/NewsList.js"))
    End Sub

#End Region

#Region "Khai báo các function method"

    Private Function GetNewsList(pPage As Integer, pNewsPerPage As Integer, pTag As String) As DataSet

        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess
        Dim nStart As Integer
        Dim nEnd As Integer

        Try
            dataAccess = Me.GetDBAccess()

            nStart = (pPage * pNewsPerPage) - (pNewsPerPage - 1)
            nEnd = pPage * pNewsPerPage

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        DAT.*"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "("
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        ROW_NUMBER() OVER(ORDER BY T_NEWS.UD_DATE DESC) AS ROW_NUM,"
            xSql = xSql & vbCrLf & "		T_NEWS.CODE,"
            xSql = xSql & vbCrLf & "		T_NEWS.NAME        AS TITLE,"
            xSql = xSql & vbCrLf & "		T_NEWS.LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_NEWS.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_NEWS.SUMMARY,"
            xSql = xSql & vbCrLf & "		T_NEWS.CONTENT,"
            xSql = xSql & vbCrLf & "		T_NEWS.TAG,"
            xSql = xSql & vbCrLf & "		T_NEWS.DEL_FLG,"
            xSql = xSql & vbCrLf & "		T_NEWS.AD_DATE,"
            xSql = xSql & vbCrLf & "		T_NEWS.UD_DATE"
            xSql = xSql & vbCrLf & "FROM "
            xSql = xSql & vbCrLf & "        T_NEWS"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_NEWS.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND     ('@TAG' = '' OR T_NEWS.TAG LIKE N'%@TAG%')"
            xSql = xSql & vbCrLf & ") DAT"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        DAT.ROW_NUM BETWEEN @START AND @END"

            xSql = Replace(xSql, "@START", ToSqlStr(nStart, SqlDataType.DataType_Integer))
            xSql = Replace(xSql, "@END", ToSqlStr(nEnd, SqlDataType.DataType_Integer))
            xSql = Replace(xSql, "@TAG", ToSqlStr(pTag, SqlDataType.DataType_String, False))

            ds = dataAccess.GetDBDataset(xSql)

            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function GetNewsCount(pTag As String) As Integer
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = Me.GetDBAccess()

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT COUNT(*) AS ROW_COUNT"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "		T_NEWS"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_NEWS.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND     ('@TAG' = '' OR T_NEWS.TAG LIKE N'%@TAG%')"

            xSql = Replace(xSql, "@TAG", ToSqlStr(pTag, SqlDataType.DataType_String, False))

            ds = dataAccess.GetDBDataset(xSql)

            If IsNothing(ds) OrElse ds.Tables(0).Rows.Count < 1 Then
                Throw New Exception("NO_DATA_FOUND")
            End If

            Return CInt(ds.Tables(0).Rows(0)("ROW_COUNT"))
        Catch ex As Exception
            Return 0
        End Try
    End Function

#End Region

End Class